//
//  PGGuideViewController.m
//
//  Created by yu on 15/11/24.
//  Copyright (c) 2015年 pg. All rights reserved.
//

#import "PGGuideViewController.h"
#import "PGTabBarController.h"
#import "LoginVC.h"

////引导页面过后跳转到登录页面
//#import "LoginVC.h"
#import "Loger3DLoginViewController.h"

#define PGMainScreenWidth [UIScreen mainScreen].bounds.size.width
#define PGMainScreenHeight [UIScreen mainScreen].bounds.size.height

@interface PGGuideViewController () <UIScrollViewDelegate>

@property (nonatomic, weak) UIPageControl *page;
@property (nonatomic, weak) UIScrollView *scrollV;
@property (nonatomic, strong) NSArray *fileImages;
@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic,retain)UIButton *button;
@end

@implementation PGGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.center = CGPointMake(self.view.center.x, self.view.frame.size.height*0.835);
    _button.bounds = CGRectMake(0, 0, 120, 40);
//    _button.backgroundColor = [UIColor redColor];
    [_button setImage:[UIImage imageNamed:@"立即体验"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(pushToMainVC) forControlEvents:UIControlEventTouchUpInside];
    
    //初始化UIScrollerView
    [self setUpScrollerView];
    
    //初始化UIPage
    [self setUpPage];
    
    //增加定时器
    [self start];
    
}

//初始化UIScrollerView
- (void)setUpScrollerView
{
    //创建scrollview
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];
    self.scrollV = scrollView;
    
    NSArray *images = @[@"qidong1-750X1334", @"qidong2-750X1334", @"qidong3-750X1334"];
    self.fileImages = images;
    
    //w. h
    CGFloat w = PGMainScreenWidth;
    CGFloat h = scrollView.frame.size.height;
    
    for (int i = 0; i < images.count; i++) {
        NSString *fileName = images[i];
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:fileName]];
        imageV.frame = CGRectMake(w * i, 0, w, h);
        [scrollView addSubview:imageV];
        
        //设置scrollView的contentsize
        scrollView.contentSize = CGSizeMake(images.count * w, h);
        
        //设置scrollView的属性
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.bounces = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        
    }
    
}

//初始化UIPage
- (void)setUpPage
{
    //创建page
    UIPageControl *page = [[UIPageControl alloc] init];
    self.page = page;
    page.center = CGPointMake(PGMainScreenWidth * 0.5, PGMainScreenHeight * 0.9);
    page.numberOfPages = self.fileImages.count;
    
    // 响应的页签点颜色
    page.currentPageIndicatorTintColor = [UIColor orangeColor];
    // 普通页签点颜色
    page.pageIndicatorTintColor = [UIColor lightGrayColor];

//    page.pageIndicatorTintColor = [UIColor colorWithRed:215/255.0 green:215/255.0 blue:215/255.0 alpha:1.0];
    page.enabled = YES;
    page.hidesForSinglePage = YES;
    
    [self.view addSubview:page];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    self.page.currentPage = offset.x / scrollView.frame.size.width;
    if (self.page.currentPage != self.fileImages.count-1) {
        [_button removeFromSuperview];
    }
    else
    {
        [self.view addSubview:_button];
    }
    
}

//开始定时器
- (void)start
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    // 主线程不管在处理什么操作，都会抽时间处理NSTimer
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}


- (void)nextPage
{
    //下个页的数量
    NSInteger nextPage = self.page.currentPage + 1;
    
    //如果下一页数量为5时，跳到发现界面
    if (nextPage == self.fileImages.count-1) {
        

        [self stop];
        
        [self.view addSubview:_button];
        
        
    }
//    NSLog(@"%d",nextPage);
    
    CGPoint offset = self.scrollV.contentOffset;
    offset.x = nextPage * PGMainScreenWidth;
    [self.scrollV setContentOffset:offset animated:YES];
}

#pragma mark --开始体验的按钮的点击事件
-(void)pushToMainVC
{
    //纯代码写的登陆界面
//    Loger3DLoginViewController *loginVC = [[Loger3DLoginViewController alloc] init];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
//    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
    
    //storyboard登陆界面
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    LoginVC *loginVC = [storyboard instantiateInitialViewController];
    [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
    
    
}

//结束定时器
- (void)stop
{
    [self.timer invalidate];
}

//当拖动开始时定时器结束
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stop];
}


//当拖动结束后定时器结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self start];
}





@end
